package cn.lg.soar.system.biz.modules.tenant.client;

import cn.lg.soar.common.util.PojoUtil;
import cn.lg.soar.core.annotations.ApiClient;
import cn.lg.soar.database.base.entity.ITenantEntity;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.model.TenantDTO;
import cn.lg.soar.system.api.service.ITenantApi;
import cn.lg.soar.system.biz.modules.tenant.entity.Tenant;
import cn.lg.soar.system.biz.modules.tenant.service.ITenantService;
import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * @author luguoxiang
 * @date 2022/1/3
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@ConditionalOnProperty(name = ITenantEntity.CONFIG_NAME, havingValue = "true")
@Hidden
@ApiClient("boot")
@RestController
@RequestMapping(ModuleConst.CLIENT_PATH + "/tenant")
public class TenantApiClient implements ITenantApi {

    @Autowired
    private ITenantService service;

    @GetMapping("listByIds")
    @Override
    public List<TenantDTO> listByIds(@RequestParam(name = "ids") List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return PojoUtil.copyToList(service.listByIds(ids), TenantDTO.class);
    }

    @GetMapping("getById")
    @Override
    public TenantDTO getById(@RequestParam(name = "id") Integer id) {
        Tenant data = service.getById(id);
        if (data == null) {
            return null;
        }
        return PojoUtil.copy(data, TenantDTO.class);
    }

}
